package classroom;

/**
 * @author yicixingchen
 * @version 1.0
 * Create by 2022/11/1 10:54
 */

public class binarySearch {
    public static void main(String[] args) {
        System.out.println(search(new int[]{1,2,3,4,5,6}, 2));
    }

    public static int search(int[] a, int k){
        int l = 0;
        int r = a.length - 1;
        while (l <= r){
            int m = (l + r) / 2;
            if (k == a[m]) return m;
            else if (k < a[m]) r=m - 1;
            else l = m + 1;
        }
        return -1;

    }
}
